package com.yyl.demo

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import com.yyl.demo.databinding.ActivityMainBinding
import com.yyl.render.render.SimpleRender

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.seekBar1.setOnSeekBarChangeListener(
            getSeek {
                binding.surfaceView.baseRender.setTransformMatrix(
                    it.toFloat(),
                    0f,
                    1.0f,
                    1.0f,
                    0,
                    0
                )
            }
        )
        binding.seekBar2.setOnSeekBarChangeListener(
            getSeek {
                binding.surfaceView.baseRender.setTransformMatrix(
                    0f,
                    0f,
                    it.toFloat() + 1.0f,
                    1.0f,
                    0,
                    0
                )
            }
        )
        binding.seekBar3.setOnSeekBarChangeListener(
            getSeek {
                binding.surfaceView.baseRender.setTransformMatrix(0.0f, 0f, 0.2f, 0.5f, it, 0)
            }
        )

//        binding.surfaceView.simpleRender.attachLife(lifecycle)
//        binding.exit.setOnClickListener {
//            binding.surfaceView.simpleRender.onDestroy()
//        }
        binding.surfaceView.attachRender(render)
    }

    val render = SimpleRender()

    fun getSeek(callback: (progress: Int) -> Unit) =
        object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                callback.invoke(progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        }

}
